package com.zllkj.mm.base;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;

import com.zllkj.mm.R;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import butterknife.BindView;

public abstract class BaseCommonActivity extends BaseAppActivity {

//    @BindView(R.id.top_top_bg)
//    View top_top_bg;

    //获取第一个fragment
    protected abstract BaseFragment getFirstFragment();

    //填充布局
    @Override
    protected int getContentViewId() {
        return R.layout.activity_fragment;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      //  top_top_bg.getLayoutParams().height = getResources().getDimensionPixelSize(getResources().getIdentifier("status_bar_height", "dimen", "android"));
        //添加第一个fragment
        if (null == getSupportFragmentManager().getFragments()
                || getSupportFragmentManager().getFragments().size() == 0) {
            BaseFragment firstFragment = getFirstFragment();
            if (null != firstFragment) {
                addFragment(firstFragment);
            } else {
                throw new NullPointerException("getFirstFragment() cannot be null");
            }
        }
    }


    //添加fragment
    protected void addFragment(BaseFragment fragment) {
        if (fragment != null) {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
//            if (animationAvailable) {
//                transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
//            }
            transaction.replace(R.id.fragment_container, fragment, fragment.getClass().getSimpleName())
                    .addToBackStack(fragment.getClass().getSimpleName())
                    .commitAllowingStateLoss();
        }
    }

    //移除fragment
    protected void removeFragment() {
        if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
            getSupportFragmentManager().popBackStack();
        } else {
            finish();
        }
    }

    //返回键事件-移除fragment
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (KeyEvent.KEYCODE_BACK == keyCode) {
            removeFragment();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);//？没有这个融云发送图片无反应？

        Log.d("acthome","Oridata-->" + data.getData().getPath());
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
        if (fragment != null && fragment instanceof BaseFragment) {
            fragment.onActivityResult(requestCode, resultCode, data);
        }
    }
}
